import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUsInfo, getUserimgId } from '@/api/user'

export default {
  namespaced: true,
  state: {
    key: getToken() || '',
    userinfo: {}
  },
  mutations: {
    updataToken(state, token) {
      state.key = token
      setToken(token)
    },
    delToken(state) {
      state.key = ''
      removeToken()
    },
    setUserInfo(state, info) {
      state.userinfo = info
    },
    delUserInfo(state) {
      state.userinfo = {}
    }

  },
  actions: {
    async  getUserInfo(x) {
      try {
        const res = await getUsInfo()
        const img = await getUserimgId(res.data.data.userId)
        // console.log(img)
        // console.log(res)
        x.commit('setUserInfo', { ...res.data.data, ...img.data.data })
      } catch (error) {
        console.log(error)
      }
      // store.commit('setUserInfo',res)
    }
  }
}
